package com.jg.xmlSchema2JsonSchema.xsd;

import cn.hutool.core.util.ObjectUtil;
import com.jg.xmlSchema2JsonSchema.xsd.util.XsdUtil;
import org.w3c.dom.Element;

public class XsElement {
    private Element element;
    // 显示名称
    private String displayName;
    // 节点名称
    private String name;
    // 节点描述
    private String annotation;
    // 节点类型
    private String type;
    private String block;
    private String defaultValue;
    private String fixed;
    private String form;
    private String id;
    private String maxOccurs;
    private String minOccurs;
    private String nillable;
    private String ref;

    public XsElement(Element element) {
        this.element = element;
        getData();
    }

    private void getData() {
        XsAnnotation xsAnnotation = XsdUtil.getAnnotation(element);
        if (xsAnnotation != null) {
            annotation = xsAnnotation.getText();
        }
        name = element.getAttribute("name");
        type = element.getAttribute("type");
        block = element.getAttribute("block");
        defaultValue = element.getAttribute("default");
        fixed = element.getAttribute("fixed");
        form = element.getAttribute("form");
        id = element.getAttribute("id");
        minOccurs = element.getAttribute("minOccurs");
        maxOccurs = element.getAttribute("maxOccurs");
        nillable = element.getAttribute("nillable");
        ref = element.getAttribute("ref");
    }

    public XsComplexType getComplexType() {
        return XsdUtil.getComplexType(element);
    }

    public XsExtension getExtension() {
        XsComplexContent xsComplexContent = getComplexContent();
        if (ObjectUtil.isNull(xsComplexContent)) {
            return null;
        }
        return xsComplexContent.getExtension();
    }

    public XsRestriction getRestriction() {
        XsComplexContent xsComplexContent = getComplexContent();
        if (ObjectUtil.isNull(xsComplexContent)) {
            return null;
        }
        return xsComplexContent.getRestriction();
    }
    public XsComplexContent getComplexContent(){
        XsComplexType xsComplexType = XsdUtil.getComplexType(element);
        if (ObjectUtil.isNull(xsComplexType)) {
            return null;
        }
        return xsComplexType.getComplexContent();
    }


    public XsUnique[] getUniques() {
        return XsdUtil.getUniques(element).toArray(new XsUnique[0]);
    }

    public String getName() {
        return name;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String newDisplayName) {
        displayName = newDisplayName;
    }


    public String getAnnotation() {
        return annotation == null ? "" : annotation;
    }


    public String getType() {
        return type;
    }

    public String getBlock() {
        return block;
    }

    public String getDefault() {
        return defaultValue;
    }

    public String getFixed() {
        return fixed;
    }

    public String getForm() {
        return form;
    }

    public String getId() {
        return id;
    }

    public String getMinOccurs() {
        return minOccurs;
    }

    public String getMaxOccurs() {
        return maxOccurs;
    }

    public String getNillable() {
        return nillable;
    }

    public String getRef() {
        return ref;
    }

    public Element getXmlElement() {
        return element;
    }
}
